package cn.fzc.yipai.service.impl;

import cn.fzc.yipai.entity.PageInfo;
import cn.fzc.yipai.entity.Response;
import cn.fzc.yipai.entity.StoreInfo;
import cn.fzc.yipai.mapper.StoreInfoMapper;
import cn.fzc.yipai.service.StoreInfoService;
import cn.fzc.yipai.utils.HttpUtils;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.TypeReference;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.collections4.CollectionUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 活动场次信息 服务实现类
 * </p>
 *
 * @author lyh
 * @since 2021-07-17
 */
@Service
public class StoreInfoServiceImpl extends ServiceImpl<StoreInfoMapper, StoreInfo> implements StoreInfoService {
    /**
     * 获取活动场次
     */
    public static final String GET_STORES_INFOS = "https://www.yihui-xh.xyz/yip/otc/advertise/getstoresinfos/1/100";


    @Autowired
    HttpUtils httpUtils;

    @Override
    public void downloadStoreInfo(String accessToken) {

        String result = httpUtils.executeGet(GET_STORES_INFOS, accessToken);
        if (StringUtils.isNotBlank(result)) {
            Response<PageInfo<StoreInfo>> pageInfoResponse = JSON.parseObject(result, new TypeReference<Response<PageInfo<StoreInfo>>>() {
            });
            List<StoreInfo> list = pageInfoResponse.getResult().getList();
            //更新数据库
            this.saveOrUpdateBatch(list);
        }
    }
}
